package com.aldjob.controllerImpl;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.project.XXXX.api.SessionContext;
import com.project.XXXX.identification.tool.ValidateTool;
import com.project.XXXX.inter.*;
import com.project.XXXX.model.*;
import com.project.XXXX.parameterType.PtJobByUserWithPage;
import com.project.XXXX.stringmodel.EEinfoAndjobString;
import com.project.XXXX.stringmodel.JobEnrollString;
import com.project.XXXX.stringmodel.PtJobAddress;
import com.project.XXXX.stringmodel.PtJobForShown;
import com.project.XXXX.stringmodel.PtJobForShownWithBlobs;
import com.project.XXXX.stringmodel.PtJobTime;
import com.project.XXXX.tools.ProjectTools;

public class EmployerPtMangaeController {

	private tbl_divisionMapper tbl_divisionMapper;
	private tbl_employeeinfoMapper tbl_employeeinfoMapper;
	private tbl_jobenrollMapper tbl_jobenrollMapper;
	private tbl_worktimeMapper tbl_worktimeMapper;
	private tbl_ptjobMapper tbl_ptjobMapper;
	private tbl_dictMapper tbl_dictMapper;
	private tbl_workplaceMapper tbl_workplaceMapper;
	private tbl_employerinfoMapper tbl_employerinfoMapper;


	public EmployerPtMangaeController(com.project.XXXX.inter.tbl_divisionMapper tbl_divisionMapper,
			com.project.XXXX.inter.tbl_employeeinfoMapper tbl_employeeinfoMapper,
			com.project.XXXX.inter.tbl_jobenrollMapper tbl_jobenrollMapper,
			com.project.XXXX.inter.tbl_worktimeMapper tbl_worktimeMapper,
			com.project.XXXX.inter.tbl_ptjobMapper tbl_ptjobMapper,
			com.project.XXXX.inter.tbl_dictMapper tbl_dictMapper,
			com.project.XXXX.inter.tbl_workplaceMapper tbl_workplaceMapper,
			com.project.XXXX.inter.tbl_employerinfoMapper tbl_employerinfoMapper) {
		super();
		this.tbl_divisionMapper = tbl_divisionMapper;
		this.tbl_employeeinfoMapper = tbl_employeeinfoMapper;
		this.tbl_jobenrollMapper = tbl_jobenrollMapper;
		this.tbl_worktimeMapper = tbl_worktimeMapper;
		this.tbl_ptjobMapper = tbl_ptjobMapper;
		this.tbl_dictMapper = tbl_dictMapper;
		this.tbl_workplaceMapper = tbl_workplaceMapper;
		this.tbl_employerinfoMapper = tbl_employerinfoMapper;
	}

	public int Post(HttpServletRequest request, HttpSession sess) {
		if (sess != null) {
			tbl_sysuser tbl_sysuser = (tbl_sysuser) sess.getAttribute("User");
			List<PtJobAddress> lworkplace;
			tbl_ptjobWithBLOBs record = new tbl_ptjobWithBLOBs();
			String json = request.getParameter("jobplace");
			Gson gson = new Gson();
			lworkplace = gson.fromJson(json, new TypeToken<List<PtJobAddress>>() {
			}.getType());
			record.setCityid(Integer.parseInt(lworkplace.get(0).getCountrycode()));
			record.setTitle(request.getParameter("title"));
			record.setPersonnum(Integer.valueOf(request.getParameter("personnum")));
			record.setSalaryunit(Integer.valueOf(request.getParameter("salaryunit")));
			record.setGenderneedid(Integer.valueOf(request.getParameter("genderneedid")));
			if (request.getParameter("billcycleid") != null)
				record.setBillcycleid(Integer.valueOf(request.getParameter("billcycleid")));
			record.setPttypeid(Integer.valueOf(request.getParameter("pttypeid")));
			if (request.getParameter("salary") != null)
				record.setSalary(BigDecimal.valueOf(Double.valueOf(request.getParameter("salary"))));
			if (request.getParameter("PhoneNum") == null)
				record.setMobile(request.getParameter("PhoneNum"));
			else
				record.setMobile(tbl_sysuser.getMobile());
			record.setRequirement(request.getParameter("requirement"));
			record.setContect(request.getParameter("content"));
			record.setCreatorid(tbl_sysuser.getMobile());
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");// 小写的mm表示的是分钟
			Date begindate = null, enddate = null;
			try {
				begindate = sdf.parse(request.getParameter("beginDate"));
				enddate = sdf.parse(request.getParameter("endDate"));
			} catch (ParseException e) {
				e.printStackTrace();
			}
			record.setBegindate(begindate);
			record.setEnddate(enddate);
			record.setTimebegin(request.getParameter("timebegin"));
			record.setTimeend(request.getParameter("timeend"));
			record.setStatus(27);
			record.setInterview(Integer.parseInt(request.getParameter("interview")));
			record.setTrain(Integer.parseInt(request.getParameter("train")));
			String id = request.getParameter("id");
			//System.out.println(record.getInterview());
			if (id != null ) {
				record.setId(Integer.parseInt(request.getParameter("id")));
				record.setModifytime(new Date());
				tbl_ptjobMapper.updateByPrimaryKeySelective(record);
				tbl_workplaceMapper.deleteBytbl_ptjob_id(record.getId());
				for (PtJobAddress p : lworkplace) {
					tbl_workplace t = new tbl_workplace();
					t.setJobplace(p.getAddress() + "&" + p.getDetailAddress());
					t.setLat(p.getLat());
					t.setLng(p.getLng());
					t.setDivisionid(p.getCountrycode());
					t.setTblPtjobId(record.getId());
					tbl_workplaceMapper.insert(t);
				}
			} else {//new
				record.setCreatetime(new Date());				
				tbl_ptjobMapper.insertSelective(record);
				List<tbl_ptjobWithBLOBs> l = tbl_ptjobMapper.SelectAllbyuserId(record.getMobile());
				record = l.get(0);
				tbl_workplaceMapper.deleteBytbl_ptjob_id(record.getId());
				for (PtJobAddress p : lworkplace) {
					tbl_workplace t = new tbl_workplace();
					t.setJobplace(p.getAddress() + "&" + p.getDetailAddress());
					t.setLat(p.getLat());
					t.setLng(p.getLng());
					t.setDivisionid(p.getCountrycode());
					t.setTblPtjobId(record.getId());
					tbl_workplaceMapper.insert(t);
				}
			}
			return 200;
		}
		return 403;
	}

	public int getCountbyStatus(HttpSession sess, String status) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (sess != null) {
			tbl_sysuser tbl_sysuser = (tbl_sysuser) sess.getAttribute("User");
			map.put("creatorid", tbl_sysuser.getMobile());
			map.put("status", status);
			return tbl_ptjobMapper.getDiffStautsPtJobSize(map);
		}
		return -1;
	}

	public List<PtJobForShown> getbyStatus(int page, String status, HttpSession sess) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<PtJobForShown> l = null;
		if (sess != null) {
			tbl_sysuser tbl_sysuser = (tbl_sysuser) sess.getAttribute("User");
			tbl_employerinfo tbl_employerinfo = tbl_employerinfoMapper.selectByPrimaryKey(tbl_sysuser.getMobile());
			List<tbl_ptjobWithBLOBs> list = null;
			int pageSize = 5;
			page = (page - 1) * pageSize;
			map.put("creatorid", tbl_sysuser.getMobile());
			map.put("status", status);
			map.put("pagesize", pageSize);
			map.put("offset", page);
			list = tbl_ptjobMapper.getDiffStautsPtJob(map);
			l = new ArrayList<PtJobForShown>();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String pttypeid;
			String cityid;
			String salaryunit;
			String billcycleid;
			int applyed,confirmed;
			for (tbl_ptjobWithBLOBs job : list) {
				map.clear();
				cityid = job.getCityid() + "";
				cityid = tbl_divisionMapper.selectCountryName(cityid).getDiming();
				salaryunit = tbl_dictMapper.selectByPrimaryKey(job.getSalaryunit()).getValue();
				pttypeid = tbl_dictMapper.selectByPrimaryKey(job.getPttypeid()).getValue();
				billcycleid = tbl_dictMapper.selectByPrimaryKey(job.getBillcycleid()).getValue();
				map.put("ptjobid", job.getId());
				map.put("status", 30);
				applyed = tbl_jobenrollMapper.getReqJobUserCount(map);
				map.clear();
				map.put("ptjobid", job.getId());
				map.put("status", 31);
				confirmed = tbl_jobenrollMapper.getReqJobUserCount(map);
				PtJobForShown ptjob = new PtJobForShown(job.getId() + "",

						job.getMobile(), job.getTitle(), pttypeid, cityid, sdf.format(job.getBegindate()),
						sdf.format(job.getEnddate()), job.getPersonnum() + "", "", job.getSalary() + "", salaryunit, "",
						"", billcycleid, sdf.format(job.getCreatetime()), job.getTimebegin(), job.getTimeend(),
						tbl_employerinfo.getLogo(),applyed,confirmed,tbl_employerinfo.getName());

				l.add(ptjob);
			}

		}
		return l;

	}

	public String getDetail(int id, HttpSession sess) {
		String j = null;
		if (sess != null) {
			tbl_ptjobWithBLOBs tbl_ptjobWithBLOBs = tbl_ptjobMapper.selectByPrimaryKey(id);
			if (tbl_ptjobWithBLOBs != null) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("ptjobid", id);
				map.put("status", 30);
				tbl_ptjobWithBLOBs.setApplyed(tbl_jobenrollMapper.getReqJobUserCount(map));
				map.clear();
				map.put("ptjobid", id);
				map.put("status", 31);
				tbl_ptjobWithBLOBs.setConfirmed(tbl_jobenrollMapper.getReqJobUserCount(map));
				List<PtJobAddress> lworkspace = new ArrayList<>();
				for (tbl_workplace x : tbl_workplaceMapper.selectJobId(id)) {
					String address[] = x.getJobplace().split("&");
					PtJobAddress p = new PtJobAddress(x.getLat(), x.getLng(), address[0], x.getDivisionid(), address[1],
							false);
					lworkspace.add(p);
				}
				Gson g = new Gson();
				String places = g.toJson(lworkspace);
				String ptJob = g.toJson(tbl_ptjobWithBLOBs);
				j = "{" + ptJob + "," + places + "}";
			}
		}
		return j;

	}

	@RequestMapping(value = "/api/employerManage/delete")
	public int deletePtJob(int id, HttpSession sess) {
		if (sess != null) {
			tbl_sysuser tbl_sysuser = (tbl_sysuser) sess.getAttribute("User");
			if (ValidateTool.IsMyJob(tbl_sysuser, tbl_ptjobMapper, id)) {
				tbl_worktimeMapper.deleteBytbl_ptjob_id(id);
				tbl_workplaceMapper.deleteBytbl_ptjob_id(id);
				tbl_ptjobMapper.deleteByPrimaryKey(id);
				return 200;
			} else
				return 409;
		} else
			return 403;

	}

	public int getAllCount(HttpSession session) {
		int t = -1;
		if (session != null) {
			tbl_sysuser tbl_sysuser = (tbl_sysuser) session.getAttribute("User");
			t = tbl_ptjobMapper.getAllbyuserIdSize(tbl_sysuser.getMobile());

		}
		return t;
	}

	public List<tbl_ptjobWithBLOBs> getAll(int page, HttpSession sess) {
		List<tbl_ptjobWithBLOBs> list = null;
		Map<String, Object> map = new HashMap<String, Object>();
		if (sess != null) {
			tbl_sysuser tbl_sysuser = (tbl_sysuser) sess.getAttribute("User");
			PtJobByUserWithPage p = new PtJobByUserWithPage();
			int pageSize = 10;
			int currentPage = page;
			currentPage = (currentPage - 1) * pageSize;
			p.setCreatorid(tbl_sysuser.getMobile());
			p.setOffset(currentPage);
			p.setPagesize(pageSize);
			list = tbl_ptjobMapper.getPtJobbyUserWithPage(p);
			for(tbl_ptjobWithBLOBs i:list){
				map.put("ptjobid", i.getId());
				map.put("status", 30);
				i.setApplyed(tbl_jobenrollMapper.getReqJobUserCount(map));
				map.put("status", 31);
				i.setConfirmed(tbl_jobenrollMapper.getReqJobUserCount(map));
			}
		}
		return list;

	}

	public int changeJobStatus(int status, int reqid, HttpSession session) {
		tbl_sysuser tbl_sysuser = (tbl_sysuser) session.getAttribute("User");
		tbl_jobenroll record = tbl_jobenrollMapper.selectByPrimaryKey(reqid);
		if (record != null) {
			int ptid = record.getPtjobid();
			if (ValidateTool.IsMyJob(tbl_sysuser, tbl_ptjobMapper, ptid)
					|| ValidateTool.IsJobEnroll(tbl_sysuser, tbl_jobenrollMapper, ptid)){
				if (record.getStateid() != status) {
					record.setStateid(status);
					record.setId(reqid);
					tbl_jobenrollMapper.updateByPrimaryKeySelective(record);
					return 200;
				}
			}
			return 409;
		}
		return 403;
	}

	public int changestatus(int status, int ptid, HttpSession sess) {
		if (sess != null) {
			tbl_sysuser tbl_sysuser = (tbl_sysuser) sess.getAttribute("User");
			tbl_ptjobWithBLOBs record = tbl_ptjobMapper.selectByPrimaryKey(ptid);
			if (record != null && (record.getStatus() != status)) {
				if (ValidateTool.IsJobEnroll(tbl_sysuser, tbl_jobenrollMapper, ptid)
						|| ValidateTool.IsMyJob(tbl_sysuser, tbl_ptjobMapper, ptid)) {
					record.setStatus(status);
					record.setId(ptid);
					tbl_ptjobMapper.updateByPrimaryKeySelective(record);
					return 200;
				} else
					return 409;
			} else
				return 409;
		} else
			return 403;

	}

	public int getRequestCountbyIdandStatus(int ptjobid, int status, HttpSession sess) {
		Map<String, Object> map = new HashMap<String, Object>();
		int i = -1;
		if (sess != null) {
			tbl_sysuser tbl_sysuser = (tbl_sysuser) sess.getAttribute("User");
			tbl_ptjob t = tbl_ptjobMapper.selectByPrimaryKey(ptjobid);
			if (t != null) {
				if (ValidateTool.IsMyJob(tbl_sysuser, tbl_ptjobMapper, ptjobid)) {
					map.put("ptjobid", ptjobid);
					map.put("status", status);
					i = tbl_jobenrollMapper.getReqJobUserCount(map);
				}
			}
		}
		return i;
	}

	public List<EEinfoAndjobString> getRequestbyIdandStatus(int page, String status, int ptjobid, HttpSession sess) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<EEinfoAndjobString> l = null;
		if (sess != null) {
			tbl_sysuser tbl_sysuser = (tbl_sysuser) sess.getAttribute("User");
			int pageSize = 10;
			int currentPage = page;
			currentPage = (currentPage - 1) * pageSize;
			tbl_ptjob t = tbl_ptjobMapper.selectByPrimaryKey(ptjobid);
			if (t != null) {
				if (ValidateTool.IsMyJob(tbl_sysuser, tbl_ptjobMapper, ptjobid)) {
					map.put("ptjobid", ptjobid);
					map.put("pagesize", pageSize);
					map.put("status", status);
					map.put("offset", currentPage);
					List<tbl_jobenroll> listJob = tbl_jobenrollMapper.getReqJobUser(map);
					l = new ArrayList<EEinfoAndjobString>();
					for (tbl_jobenroll job : listJob) {
						SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
						tbl_employeeinfo info = tbl_employeeinfoMapper.selectByPrimaryKey(job.getMobile());

						int age = (new Date().getYear()) - info.getBirthday().getYear();
						EEinfoAndjobString e = new EEinfoAndjobString(info.getRealname(), info.getUsername(),
								info.getMobile(), info.getMajor(), info.getEmail(), info.getMajor(), info.getSchool(),
								info.getTellus(), info.getTheyear(), info.getWechatid(),
								sdf.format(job.getHappendate()), age + "", job.getId() + "", job.getStateid() + "");
						l.add(e);
					}
				}
			}

		}
		return l;

	}
}